#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "Base64.h"
static const char *ALPHA_BASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

char *encodeBase64(const char *buf, const long size, char *base64Char) {
	int a = 0;
	int i = 0;
	while (i < size) {
		char b0 = buf[i++];
		char b1 = (i < size) ? buf[i++] : 0;
		char b2 = (i < size) ? buf[i++] : 0;

		int int63 = 0x3F; //  00111111
		int int255 = 0xFF; // 11111111
		base64Char[a++] = ALPHA_BASE[(b0 >> 2) & int63];
		base64Char[a++] = ALPHA_BASE[((b0 << 4) | ((b1 & int255) >> 4)) & int63];
		base64Char[a++] = ALPHA_BASE[((b1 << 2) | ((b2 & int255) >> 6)) & int63];
		base64Char[a++] = ALPHA_BASE[b2 & int63];
	}
	switch (size % 3) {
	case 1:
		base64Char[--a] = '=';
	case 2:
		base64Char[--a] = '=';
	}
	return base64Char;
}

char *decodeBase64(const char *base64Char, const long base64CharSize, char *originChar, long originCharSize) {
	int toInt[128] = { -1 };
	for (int i = 0; i < 64; i++) {
		toInt[ALPHA_BASE[i]] = i;
	}
	int int255 = 0xFF;
	int index = 0;
	for (int i = 0; i < base64CharSize; i += 4) {
		int c0 = toInt[base64Char[i]];
		int c1 = toInt[base64Char[i + 1]];
		originChar[index++] = (((c0 << 2) | (c1 >> 4)) & int255);
		if (index >= originCharSize) {
			return originChar;
		}
		int c2 = toInt[base64Char[i + 2]];
		originChar[index++] = (((c1 << 4) | (c2 >> 2)) & int255);
		if (index >= originCharSize) {
			return originChar;
		}
		int c3 = toInt[base64Char[i + 3]];
		originChar[index++] = (((c2 << 6) | c3) & int255);
	}
	return originChar;
}